<?php
$outerHtml = "";

//$menuTitle = $menuArray['name'];
//$menuTitleIcon = $menuArray['icon'];
foreach ($menuArray['menu'] as $outerItem) {
    $outerName = $outerItem["name"];
    $outerIcon = $outerItem["icon"];
    $outerFile = $outerItem["file"];
    $outerOnclick = $outerItem["onclick"];
    $outerActiveArray = $outerItem["active"];
    $outerParam = $outerItem["param"];
    $outerIsActive = false;

    $outerFileList = explode("/", strtolower($outerFile));
    $outerController = $outerFileList[0];
    $outerAction = count($outerFileList) == 1 ? "index" : $outerFileList[1];

    // $sitelang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    $insideList = $outerItem["sub"];
    $insideCount = count($insideList);
    $insideHtml = null;
    if(!empty($insideList)){

        foreach ($insideList as $insideItem) {

            $insideName = $insideItem["name"];
            $insideFile = $insideItem["file"];
            $insideIcon = $insideItem["icon"];
            $insideActiveArray = $insideItem["active"];
            $insideParam = $insideItem["param"];
            $insideIsActive = false;
            if (is_array($insideFile)) {
                $threeHtml = null;
                foreach ($insideFile as $threeItem) {
                    $threeName = $threeItem["name"];
                    $threeFile = $threeItem["file"];
                    $threeIcon = $threeItem["icon"];
                    $threeActiveArray = $threeItem["active"];
                    $threeParam = $threeItem["param"];
                    $threeIsActive = false;

                    $threeFileList = explode("/", strtolower($threeFile));

                    $threeController = $threeFileList[0];
                    $threeAction = count($threeFileList) == 1 ? "index" : $threeFileList[1];

                    $threeIsActive = $tm_active_controllerName == $threeController && $tm_active_actionName == $threeAction;

                    if ($threeIsActive) {
                        if (count($tm_active_param) > 0 && count($threeParam) > 0) {
                            $threeIsActive = count(array_intersect_assoc($tm_active_param, $threeParam)) != 0;
                        }
                    }

                    if (!$threeIsActive) {
                        foreach ($threeActiveArray as $trItem) {
                            $trItemList = explode("/", strtolower($trItem));
                            $trItemController = $trItemList[0];
                            $trItemAction = count($trItemList) == 1 ? "index" : $trItemList[1];

                            if ($tm_active_controllerName == $trItemController && $tm_active_actionName == $trItemAction) {
                                $threeIsActive = true;
                                $insideIsActive = true;
                                $outerIsActive = true;
                                break;
                            }
                        }
                    } else {
                        $insideIsActive = true;
                        $outerIsActive = true;
                    }

                    if ($threeIsActive) $threeHtml .= "<li class='active'>";
                    else $threeHtml .= "<li>";

                    $urlPath = U($threeFile, $threeParam);
                    $threeHtml .= "<a href='" . $urlPath . "'><i class='menu-icon fa fa-caret-right'></i>" . $threeName . "</a><b class='arrow'></b></li>";
                }

                if ($insideIsActive) $insideHtml .= "<li class='active hsub open'>";
                else $insideHtml .= "<li class='hsub'>";
                if (empty($threeHtml))
                    $insideHtml .= "<a href='javascript:;'><i class='menu-icon fa fa-caret-right'></i>" . $insideName . "</a><b class='arrow'></b>" . $threeHtml . "</li>";
                else
                    $insideHtml .= "<a href='javascript:;' class='dropdown-toggle'><i class='menu-icon fa fa-caret-right'></i>" . $insideName . "<b class='arrow fa fa-angle-down'></b></a>
                                        <b class='arrow'></b><ul class='submenu'>" . $threeHtml . "</ul></li>";
            } else {

                $insideFileList = explode("/", strtolower($insideFile));
                $insideController = $insideFileList[0];
                $insideAction = count($insideFileList) == 1 ? "index" : $insideFileList[1];

                $insideIsActive = $tm_active_controllerName == $insideController && $tm_active_actionName == $insideAction;

                if ($insideIsActive) {
                    if (count($tm_active_param) > 0 && count($insideParam) > 0) {
                        $insideIsActive = count(array_intersect_assoc($tm_active_param, $insideParam)) != 0;
                    }
                }

                if (!$insideIsActive) {
                    if(!empty($insideActiveArray)){
                        foreach ($insideActiveArray as $inItem) {
                            if (is_array($inItem)) {
                                $inFile = $inItem["file"];
                                $inParam = $inItem["param"];

                                $inItemList = explode("/", strtolower($inFile));
                                $inItemController = $inItemList[0];
                                $inItemAction = count($inItemList) == 1 ? "index" : $inItemList[1];

                                if ($tm_active_controllerName == $inItemController && $tm_active_actionName == $inItemAction) {
                                    $insideIsActive = true;
                                }
                                if ($insideIsActive) {
                                    if (count($tm_active_param) > 0 && count($inParam) > 0) {
                                        $insideIsActive = count(array_intersect_assoc($tm_active_param, $inParam)) != 0;
                                    }
                                }

                                if ($insideIsActive) {
                                    $outerIsActive = true;
                                    break;
                                }
                            } else {
                                $inItemList = explode("/", strtolower($inItem));
                                $inItemController = $inItemList[0];
                                $inItemAction = count($inItemList) == 1 ? "index" : $inItemList[1];

                                if ($tm_active_controllerName == $inItemController && $tm_active_actionName == $inItemAction) {
                                    $insideIsActive = true;
                                    $outerIsActive = true;
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    $outerIsActive = true;
                }

                if ($insideIsActive) $insideHtml .= "<li class='active'>";
                else $insideHtml .= "<li>";

                $urlPath = U($insideFile, $insideParam);
                $insideHtml .= "<a href='" . $urlPath . "'><i class='menu-icon fa fa-caret-right'></i>" . $insideName . "</a><b class='arrow'></b></li>";
            }
        }
    }

    if (!$outerIsActive) {
        $outerIsActive = $tm_active_controllerName == $outerController && $tm_active_actionName == $outerAction;

        if ($outerIsActive) {
            if (count($tm_active_param) > 0 && count($outerParam) > 0) {
                $outerIsActive = count(array_intersect_assoc($tm_active_param, $outerParam)) != 0;
            }
        }

        if (!$outerIsActive ) {
            if(!empty($outerActiveArray)){
                foreach ($outerActiveArray as $ouItem) {
                    if (is_array($ouItem)) {
                        $ouFile = $ouItem["file"];
                        $ouParam = $ouItem["param"];

                        $inItemList = explode("/", strtolower($ouFile));
                        $inItemController = $inItemList[0];
                        $inItemAction = count($inItemList) == 1 ? "index" : $inItemList[1];

                        if ($tm_active_controllerName == $inItemController && $tm_active_actionName == $inItemAction) {
                            $outerIsActive = true;
                        }
                        if ($outerIsActive) {
                            if (count($tm_active_param) > 0 && count($inParam) > 0) {
                                $outerIsActive = count(array_intersect_assoc($tm_active_param, $ouParam)) != 0;
                            }
                        }

                        if ($outerIsActive) {
                            break;
                        }
                    } else {
                        $ouItemList = explode("/", strtolower($ouItem));
                        $ouController = $ouItemList[0];
                        $ouAction = count($ouItemList) == 1 ? "index" : $ouItemList[1];

                        if ($tm_active_controllerName == $ouController && $tm_active_actionName == $ouAction) {
                            $outerIsActive = true;
                            break;
                        }
                    }
                }
            }
        }
    }
    $dropDown = null;
    $dropDownIcon = null;
    if ($insideCount > 0) {
        $dropDown = "class='dropdown-toggle'";
        $dropDownIcon = "<b class='arrow fa fa-angle-down'></b>";
    }
    if (isset($outerIcon) && !is_null($outerIcon)) $outerIcon = "<i class='" . $outerIcon . "'></i>";

    if ($outerIsActive) {
        if ($insideCount > 0)
            $outerHtml .= "<li class='active open'>";
        else
            $outerHtml .= "<li class='active'>";
    } else $outerHtml .= "<li>";

    //$headmenu = $sitelang == 'en' ? $outerName : $lang[$outerName];

    $urlPath = U($outerFile, $outerParam);

    $thisUrl = $outerOnclick == '' ? $urlPath : "javascript:;";
    $clickUrl = $outerOnclick != '' ? "onclick='openDialog(\"".U($outerOnclick, $outerParam)."\", \"".$outerName."\");'" : "";
    $outerHtml .= "<a href='" . $thisUrl . "'" . $dropDown . " ".$clickUrl.">" . $outerIcon . "<span class='menu-text'>" . $outerName . "</span>" . $dropDownIcon . "</a><b class='arrow'></b>";
    if (!is_null($insideHtml)) $outerHtml .= "<ul class='submenu'>" . $insideHtml . "</ul></li>";
    else $outerHtml .= "</li>";
}
echo "<ul class='nav nav-list'>" . $outerHtml . "</ul>";